package org.authsum.stitches.wicket.contentdef;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.Form;
import org.authsum.stitches.external.ContentDefHolder;
import org.authsum.stitches.external.ContentDefRequest;
import org.authsum.stitches.external.ContentDefResponse;
import org.authsum.stitches.wicket.BaseLandingPanel;
import org.authsum.stitches.wicket.SearchParms;
import org.authsum.stitches.wicket.contentdef.detail.ContentDefPanel;
import org.authsum.stitches.wicket.contentdef.table.ContentDefTable;

/*
 * am i being too clever, I am using this panel to search and edit content defs, but you can also "pick" one
 * this is accomplished via  2 different listeners
 */
public class ContentDefsPanel extends BaseLandingPanel implements
		ContentDefSelectionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ContentDefSelectionListener contentDefListener = null;

	// public abstract ContentDefSelectionListener
	// getContentDefSelectionListener();

	public ContentDefsPanel(String id,
			ContentDefSelectionListener contentDefListener) {
		super(id);
		this.contentDefListener = contentDefListener;
		ContentDefHolder cdh = new ContentDefHolder();

		detailPanel = new ContentDefPanel("detailPanel", cdh, this);// ,PackageAttractionModal);

		postInit();

	}

	public ContentDefsPanel(String id) {
		super(id);

		ContentDefHolder cdh = new ContentDefHolder();

		detailPanel = new ContentDefPanel("detailPanel", cdh, this);// ,PackageAttractionModal);

		contentDefListener = this;
		postInit();

	}

	public void contentDefSelected(AjaxRequestTarget target,
			ContentDefHolder contentDefHolder) {
		editContentDef(target, contentDefHolder);

	}

	private void editContentDef(ContentDefHolder contentDefHolder) {
		System.out.println("editContentDef2");
		detailPanel = new ContentDefPanel("detailPanel", contentDefHolder, this);
		editDetailObject();
	}

	protected void initTable(List list) {
		resultsTable = new ContentDefTable("resultsTable", list,
				contentDefListener);
		updateContainer.addOrReplace(resultsTable);
	}

	private static Log logger = LogFactory.getLog(ContentDefsPanel.class);

	public void editContentDef(AjaxRequestTarget target, ContentDefHolder cdf) {
		// TODO Auto-generated method stub
		// ImageContent imageContent = image.getImageContent();
		// image has been selected
		System.out.println("editContentDef");
		System.out.println("Editing cdf " + cdf.getName());
		try {
			// need to get the freshing imagecontent, the one that was passed in
			// is from search results
			ContentDefRequest cdrequest = new ContentDefRequest();
			cdrequest.setContentDefId(cdf.getId());
			ContentDefResponse cdresponse = getStitchesService()
					.findContentDefById(cdrequest);
			editContentDef(cdresponse.getContentDefHolders()
					.getContentDefHolder().get(0));
			// backToSearchResults.setVisible(true);
			target.addComponent(updateContainer);
		} catch (Exception e) {
			e.printStackTrace();
			error("An error occurred while trying to edit this event.");
			error(e.getMessage());
			// target.addComponent(feedbackPanel);
		}

	}

	private void refreshTable(ContentDefResponse contentDefResponse) {
		initTable(contentDefResponse.getContentDefHolders()
				.getContentDefHolder());

		backToSearchResults.setVisible(false);
		detailPanel.setVisible(false);
		centerDefaultPanel.setVisible(false);
	}

	protected void refreshTable(AjaxRequestTarget target,
			ContentDefResponse contentDefResponse) {

		refreshTable(contentDefResponse);
		// DMIResult dmiResult = getDMIResult(dmiRequest);
		// backToSearchResults.setVisible(false);
		// detailPanel.setVisible(false);

		// initTable(dmiResult);

		target.addComponent(updateContainer);
		// target.addComponent(detailPanel);
		target.addComponent(backToSearchResults);
		target.addComponent(feedbackPanel);
		// target.addComponent(this);

	}

	@Override
	public void createDetailObject() {
		ContentDefHolder cdh = new ContentDefHolder();

		editContentDef(cdh);

	}

	@Override
	public void performSearch(AjaxRequestTarget target, Form form) {
		SearchParms searchParms = (SearchParms) searchForm.getModel()
				.getObject();

		ContentDefRequest contentDefRequest = new ContentDefRequest();
		contentDefRequest.setKeywords(searchParms.getKeywords());

		ContentDefResponse contentDefResponse = getStitchesService()
				.findContentDefs(contentDefRequest);

		// DMIResult dmiResult= getDMIResult(dmiRequest);
		refreshTable(target, contentDefResponse);
		searchTips.setVisible(contentDefResponse.getContentDefHolders()
				.getContentDefHolder().isEmpty());

	}

}
